package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.Transport;
import com.kede.app.service.TransportService;
import com.kede.app.service.dto.TransportQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author Pr.li
* @date 2023-10-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "transport管理")
@RequestMapping("/api/transport")
public class TransportController {

    private final TransportService transportService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('transport:list')")
    public void exportTransport(HttpServletResponse response, TransportQueryCriteria criteria) throws IOException {
        transportService.download(transportService.queryAll(criteria), response);
    }

    /**
    * 查询transport
    **/
    @GetMapping
    @Log("查询transport")
    @ApiOperation("查询transport")
    @PreAuthorize("@el.check('transport:list')")
    public ResponseEntity<Object> queryTransport(TransportQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(transportService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增transport
    **/
    @PostMapping
    @Log("新增transport")
    @ApiOperation("新增transport")
    @PreAuthorize("@el.check('transport:add')")
    public ResponseEntity<Object> createTransport(@Validated @RequestBody Transport resources){
        return new ResponseEntity<>(transportService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改transport
    **/
    @PutMapping
    @Log("修改transport")
    @ApiOperation("修改transport")
    @PreAuthorize("@el.check('transport:edit')")
    public ResponseEntity<Object> updateTransport(@Validated @RequestBody Transport resources){
        transportService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除transport
    **/
    @DeleteMapping
    @Log("删除transport")
    @ApiOperation("删除transport")
    @PreAuthorize("@el.check('transport:del')")
    public ResponseEntity<Object> deleteTransport(@RequestBody Integer[] ids) {
        transportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}